home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / GLUT / progs / sgi-stereo / stereo-plane.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  5.0 KB  |  208 lines

  1.  
  2. /* This is an OpenGL/GLUT implementation of the "plane" stereo  
  3.    demonstration program found in Appendix 1 of "The CrystalEyes  Handbook"
  4.    by Lenny Lipton, 1991, StereoGraphics Corp.   */
  5.  
  6. /* Ported to OpenGL/GLUT by Mike Blackwell, mkb@cs.cmu.edu, Oct. 1995.  */
  7.  
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <GL/glu.h>
  11. #include <GL/glut.h>
  12. #include "fullscreen_stereo.h"
  13.  
  14. #define SPEED        100      /* How often to update rotation - milliseconds */
  15. #define STEP        1.0       /* How much to rotate - degrees */
  16.  
  17. double position = 0;    /* Rotation of plane */
  18.  
  19. /* Clean up and quit */
  20. void
  21. all_done(void)
  22. {
  23.   stop_fullscreen_stereo();
  24.   exit(0);
  25. }
  26.  
  27. void
  28. timer(int value)
  29. {
  30.   position -= STEP;
  31.   if (position < 0.0)
  32.     position = 360.0 - STEP;
  33.   glutPostRedisplay();
  34.   glutTimerFunc(SPEED, timer, 0);
  35. }
  36.  
  37. void
  38. keyboard(unsigned char key, int x, int y)
  39. {
  40.   switch (key) {
  41.   case '\033':
  42.     all_done();
  43.     break;
  44.   default:
  45.     putchar('\007');
  46.     fflush(stdout);
  47.     break;
  48.   }
  49. }
  50.  
  51. /* This routine performs the perspective projection for one eye's subfield.
  52.    The projection is in the direction of the negative z axis.
  53.  
  54.    xmin, ymax, ymin, ymax = the coordinate range, in the plane of zero
  55.    parallax setting, that will be displayed on the screen. The ratio between
  56.    (xmax-xmin) and (ymax-ymin) should equal the aspect ration of the display.
  57.  
  58.    znear, zfar = the z-coordinate values of the clipping planes.
  59.  
  60.    zzps = the z-coordinate of the plane of zero parallax setting.
  61.  
  62.    dist = the distance from the center of projection to the plane of zero
  63.    parallax.
  64.  
  65.    eye = half the eye separation; positive for the right eye subfield,
  66.    negative for the left eye subfield. */
  67.  
  68. void
  69. stereoproj(float xmin, float xmax, float ymin, float ymax,
  70.   float znear, float zfar, float zzps, float dist, float eye)
  71. {
  72.   float xmid, ymid, clip_near, clip_far, top, bottom, left, right, dx, dy, n_over_d;
  73.  
  74.   dx = xmax - xmin;
  75.   dy = ymax - ymin;
  76.  
  77.   xmid = (xmax + xmin) / 2.0;
  78.   ymid = (ymax + ymin) / 2.0;
  79.  
  80.   clip_near = dist + zzps - znear;
  81.   clip_far = dist + zzps - zfar;
  82.  
  83.   n_over_d = clip_near / dist;
  84.  
  85.   top = n_over_d * dy / 2.0;
  86.   bottom = -top;
  87.   right = n_over_d * (dx / 2.0 - eye);
  88.   left = n_over_d * (-dx / 2.0 - eye);
  89.  
  90.   glMatrixMode(GL_PROJECTION);
  91.   glLoadIdentity();
  92.   glFrustum(left, right, bottom, top, clip_near, clip_far);
  93.  
  94.   glTranslatef(-xmid - eye, -ymid, -zzps - dist);
  95. }
  96.  
  97. void
  98. draw_airplane(void)
  99. {
  100.   static float airplane[9][3] =
  101.   {
  102.     {0.0, 0.5, -4.5},
  103.     {3.0, 0.5, -4.5},
  104.     {3.0, 0.5, -3.5},
  105.     {0.0, 0.5, 0.0},
  106.     {0.0, 0.5, 3.25},
  107.     {0.0, -0.5, 5.5},
  108.     {-3.0, 0.5, -3.5},
  109.     {-3.0, 0.5, -4.5},
  110.     {0.0, -0.5, -4.5}
  111.   };
  112.  
  113.   glColor3ub(0xb0, 0x30, 0xff);  /* Purple color */
  114.  
  115.   glBegin(GL_LINE_LOOP);
  116.   glVertex3fv(airplane[6]);
  117.   glVertex3fv(airplane[7]);
  118.   glVertex3fv(airplane[1]);
  119.   glVertex3fv(airplane[2]);
  120.   glVertex3fv(airplane[4]);
  121.   glEnd();
  122.  
  123.   glBegin(GL_LINE_LOOP);
  124.   glVertex3fv(airplane[0]);
  125.   glVertex3fv(airplane[4]);
  126.   glVertex3fv(airplane[5]);
  127.   glVertex3fv(airplane[8]);
  128.   glEnd();
  129.  
  130.   glBegin(GL_LINE_STRIP);
  131.   glVertex3fv(airplane[6]);
  132.   glVertex3fv(airplane[3]);
  133.   glVertex3fv(airplane[2]);
  134.   glEnd();
  135. }
  136.  
  137. /* This routine puts a stereo image of a paper airplane onto the screen */
  138. void
  139. redraw(void)
  140. {
  141.   /* Draw left subfield */
  142.   stereo_left_buffer();
  143.  
  144.   glClearColor(0.07, 0.07, 0.07, 0.00);
  145.   glClear(GL_COLOR_BUFFER_BIT);
  146.  
  147.   /* Z-coordinate of plane of zero parallax is 0.0. In that plane, the coord
  148.      range drawn to the screen will be
  149.  
  150.      (-6.0 to 6.0, -4.8 to 4.8).
  151.  
  152.      Z-coordinate clipping planes are -6.0 and 6.0. The eyes are set at world
  153.      coord distance 14.5 from the plane of zero parallax, and the eye
  154.      separation is 0.62 in world coords. These two values were calculated
  155.      using equations 11 to 15, and 17 to 19 in chapter 5. */
  156.  
  157.   stereoproj(-6.0, 6.0, -4.8, 4.8, 6.0, -6.0, 0.0, 14.5, -0.31);
  158.  
  159.   glMatrixMode(GL_MODELVIEW);
  160.   glLoadIdentity();
  161.   glRotatef((float) position, 0.0, 1.0, 0.0);
  162.   glRotatef(-10.0, 1.0, 0.0, 0.0);
  163.   draw_airplane();
  164.   glFlush();
  165.  
  166.   /* Draw right subfield */
  167.   stereo_right_buffer();
  168.  
  169.   glClearColor(0.07, 0.07, 0.07, 0.00);
  170.   glClear(GL_COLOR_BUFFER_BIT);
  171.  
  172.   /* Same as above stereoproj() call, except that eye arg is positive */
  173.   stereoproj(-6.0, 6.0, -4.8, 4.8, 6.0, -6.0, 0.0, 14.5, 0.31);
  174.  
  175.   glMatrixMode(GL_MODELVIEW);
  176.   glLoadIdentity();
  177.   glRotatef((float) position, 0.0, 1.0, 0.0);
  178.   glRotatef(-10.0, 1.0, 0.0, 0.0);
  179.   draw_airplane();
  180.   glFlush();
  181.  
  182.   glutSwapBuffers();    /* Update screen */
  183. }
  184.  
  185. int
  186. main(int argc, char *argv[])
  187. {
  188.   glutInit(&argc, argv);
  189.   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  190.   glutCreateWindow("GLUT-based SGI hack stereo demo");
  191.   glutFullScreen();
  192.   glutSetCursor(GLUT_CURSOR_NONE);
  193.   start_fullscreen_stereo();
  194.  
  195.   glutDisplayFunc(redraw);
  196.   glutKeyboardFunc(keyboard);
  197.   glutTimerFunc(SPEED, timer, 0);
  198.  
  199.   glEnable(GL_LINE_SMOOTH);
  200.   glEnable(GL_BLEND);
  201.   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  202.   glHint(GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
  203.   glLineWidth(1.5);
  204.  
  205.   glutMainLoop();
  206.   return 0;             /* ANSI C requires main to return int. */
  207. }
  208.